package com.platform.controller;

import com.platform.entity.InviteEntity;
import com.platform.entity.TeamWithUserEntity;
import com.platform.service.InviteService;
import com.platform.service.TeamWithUserService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 用户邀请关联表Controller
 *  推广管理:邀请关系
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2019-02-21 17:57:34
 */
@RestController
@RequestMapping("invite")
public class InviteController {
    @Autowired
    private InviteService inviteService;

    @Autowired
    private TeamWithUserService teamWithUserService;
    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("invite:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<InviteEntity> inviteList = inviteService.queryList(query);
        int total = inviteService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(inviteList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("invite:info")
    public R info(@PathVariable("id") Long id) {
        InviteEntity invite = inviteService.queryObject(id);

        return R.ok().put("invite", invite);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("invite:save")
    public R save(@RequestBody InviteEntity invite) {
        inviteService.save(invite);

        //获取邀请人的团队编号
        Long uid = invite.getUid();
        TeamWithUserEntity teamWithUserEntity = teamWithUserService.queryObject(uid);
        Long inviteTeamId = teamWithUserEntity.getSubTeamId();
        //获取被邀请人编号
        Long inviteUid = invite.getInviteUid();
        //新建空的TeamWithUserEntity对象 存入数据 并进行更新
        TeamWithUserEntity teamWithUser = new TeamWithUserEntity();
        teamWithUser.setParentTeamId(inviteTeamId);
        teamWithUser.setUserId(inviteUid);
        teamWithUserService.update(teamWithUser);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("invite:update")
    public R update(@RequestBody InviteEntity invite) {
        inviteService.update(invite);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("invite:delete")
    public R delete(@RequestBody Long[] ids) {
        inviteService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<InviteEntity> list = inviteService.queryList(params);

        return R.ok().put("list", list);
    }
}
